﻿using AutoMapper;
using B.S.BasicData.API.Read.Application.Command.User;
using B.S.BasicData.API.Read.DTOS.UserRoleMenu;
using B.S.BasicData.Domain.Entities.UserRoleMenu;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.API.Read.Application.Handler.User
{
    /// <summary>
    /// 根据部门Id获取相对应的用户信息请求命令处理类
    /// </summary>
    public class GetUserByDepartmentIdHandler:IRequestHandler<GetUserByDepartmentIdCommand,ApiResult<List<UserDTO>>>
    {
        IBaseRepository<UserModel> userRep;
        IMapper mapper;

        public GetUserByDepartmentIdHandler(IBaseRepository<UserModel> userRep, IMapper mapper)
        {
            this.userRep = userRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<UserDTO>>> Handle(GetUserByDepartmentIdCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<UserDTO>> res = new ApiResult<List<UserDTO>>();
                var users = userRep.GetAll().Where(x => x.IsDeleted == false&&x.DepartmentId==request.departmentId);
                var entitys = mapper.Map<List<UserDTO>>(users);
                res.Msg = "根据部门Id获取用户信息";
                res.Code = ApiEnum.Success;
                res.Data = entitys.ToList();
                return Task.FromResult(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
